package com.viphuli.app.tool.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import butterknife.Bind;

import com.offroader.core.OffRoaderActivity;
import com.offroader.utils.LogUtils;
import com.offroader.utils.PhoneUtils;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyApp;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.common.PerferencesHelper;

/**
 * 闪屏
 * 
 * @author li.li
 *
 */
public class WelcomeActivity extends OffRoaderActivity implements AnimationListener {

	@Bind(R.id.id_welcome)
	protected ViewGroup welcome;

	@Override
	public int getContentView() {
		return R.layout.act_welcome;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//开始友盟推送
		PushAgent mPushAgent = PushAgent.getInstance(this);
		mPushAgent.enable();

		String device_token = UmengRegistrar.getRegistrationId(this);
		
		//获得友盟推送token
		mPushAgent.setRegisterCallback(new IUmengRegisterCallback() {

			@Override
			public void onRegistered(String deviceToken) {

				AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(WelcomeActivity.this);

				if (tokenKeeper.isLogin()) {

					PushAgent mPushAgent = PushAgent.getInstance(WelcomeActivity.this);

					try {
						mPushAgent.addAlias(tokenKeeper.getUser().getUid(), "uid");
					} catch (Throwable e) {
						LogUtils.error(e.getMessage(), e);
					}

				}

			}
		});

	}

	@Override
	public void initView() {

		Animation anim = AnimationUtils.loadAnimation(this, R.anim.splash_alpha_in);
		anim.setAnimationListener(this);

		welcome.startAnimation(anim);

	}

	@Override
	public void initData() {
	}

	@Override
	public void onAnimationEnd(Animation anim) {

		int guideVCode = PerferencesHelper.getInstance().readInt(GuideActivity.GUIDE_V_CODE_KEY);

		if (PhoneUtils.getAppVersionCode(MyApp.getInstance()) > guideVCode) {//去引导页

			Intent intent = new Intent(this, GuideActivity.class);
			startActivity(intent);

		} else {//去首页

			MyPageHelper.home.showMyPage(this);

		}

	}

	@Override
	public void onAnimationRepeat(Animation anim) {

	}

	@Override
	public void onAnimationStart(Animation anim) {

	}

}
